package com.eascs.pbafterloan.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.admin.api.form.O2oAfterLoanForm;
import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.pbafterloan.service.O2oPBAfterService;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.page.SelectDataOption;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;

@RestController
@RequestMapping("/pbrafterloan")
public class O2oBPAfterLoanRestController {
	@Autowired
	O2oPBAfterService service;

	@RequestMapping("/o2o")
	public PaginationRestResult o2o(O2oAfterLoanForm entity) {
		String o2ourl = "/pbrafterloan/o2o";
		if (entity != null) {
			String billCode = entity.getBillCode() == null ? "" : entity.getBillCode();
			String bankLoan = entity.getBankLoan() == null ? "" : entity.getBankLoan();
			String operName = entity.getOperName() == null ? "" : entity.getOperName();
			String loanUserName = entity.getLoanUserName() == null ? "" : entity.getLoanUserName();
			String loanDateStart = entity.getLoanDateStart() == null ? null : entity.getLoanDateStart();
			String loanDateEnd = entity.getLoanDateEnd() == null ? null : entity.getLoanDateEnd();
			String createUserName = entity.getCreateUserName() == null ? "" : entity.getCreateUserName();
			String createDateStart = entity.getCreateDateStart() == null ? null : entity.getCreateDateStart();
			String createDateEnd = entity.getCreateDateEnd() == null ? null : entity.getCreateDateEnd();
			String dateDueStart = entity.getDateDueStart() == null ? null : entity.getDateDueStart();
			String dateDueEnd = entity.getDateDueEnd() == null ? null : entity.getDateDueEnd();
			String ltdName = entity.getLtdName() == null ? "" : entity.getLtdName();
			String ltd = entity.getLtd() == null ? "" : entity.getLtd();
			String loanType = entity.getLoanType() == null ? "" : entity.getLoanType();
			String province = entity.getProvince() == null ? "" : entity.getProvince();
			String gdStatus = entity.getGdStatus() == null ? "" : entity.getGdStatus();
			String repaystatus = entity.getRepaystatus() == null ? "" : entity.getRepaystatus();
			String idc = entity.getIdc() == null ? "" : entity.getIdc();
			String regionalName = entity.getRegionalName()== null ? "" : entity.getRegionalName();
			String brandName = entity.getBrandName()== null ? "" : entity.getBrandName();
			String partnerName = entity.getPartnerName()== null ? "" : entity.getPartnerName();
			String hostProvince = entity.getHostProvince()== null ? "" : entity.getHostProvince();
			o2ourl = UrlUtil.appendUrl(o2ourl, "billCode", billCode);
			o2ourl = UrlUtil.appendUrl(o2ourl, "bankLoan", bankLoan);
			o2ourl = UrlUtil.appendUrl(o2ourl, "operName", operName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanUserName", loanUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateStart", loanDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanDateEnd", loanDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createUserName", createUserName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateStart", createDateStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "createDateEnd", createDateEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueStart", dateDueStart);
			o2ourl = UrlUtil.appendUrl(o2ourl, "dateDueEnd", dateDueEnd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltdName", ltdName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "ltd", ltd);
			o2ourl = UrlUtil.appendUrl(o2ourl, "loanType", loanType);
			o2ourl = UrlUtil.appendUrl(o2ourl, "province", province);
			o2ourl = UrlUtil.appendUrl(o2ourl, "gdStatus", gdStatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "repaystatus", repaystatus);
			o2ourl = UrlUtil.appendUrl(o2ourl, "idc", idc);
			o2ourl = UrlUtil.appendUrl(o2ourl, "regionalName", regionalName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "brandName", brandName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "partnerName", partnerName);
			o2ourl = UrlUtil.appendUrl(o2ourl, "hostProvince", hostProvince);
			o2ourl = o2ourl + "&o2oIndex=";
		} else {
			o2ourl = o2ourl + "?o2oIndex=";
		}
		entity.setPageSize(10);
		PageResult<Object[]> pageResult = service.getO2oAfterLoanByPageNew(entity);
		@SuppressWarnings("unused")
		List<Object[]> data  = pageResult.getData();
		String titles[] = {"贷款申请单号", "放款银行", "产品类型", "经营平台","省区", "借款人姓名", "放款金额(万元)", "放款日期", "到期日期", "信贷经理","品牌","渠道名称"};
		Button[] titleButtons = new Button[]{ButtonHelper.createOpenButton(null, "/detail?loanapplyNo=")};
		SelectDataOption[] selectDataOptions = { null,new SelectDataOption("BANKTYPEO2O"), new SelectDataOption("OTO_LOAN_TYPE"), null, null, null, null, null,null,null};
		PaginationInfo o2oPagingInfo = new PaginationInfo(pageResult, titles, o2ourl);
		o2oPagingInfo.setBuildPagingNavigation(true);
		o2oPagingInfo.setFirstRenderColumnIndex(1);
		o2oPagingInfo.setTitleButtons(titleButtons);
		o2oPagingInfo.setSelectDataOptions(selectDataOptions);
		o2oPagingInfo.setFormats(new Format[] { null, null, null, null, null, null, new DecimalFormat(",##0.00"),null, null, null });
		o2oPagingInfo.setRendTo("#result_list");
		ViewContext.put("vo", entity);
		return PaginationRestResult.buildPaginationRestResult(o2oPagingInfo);
	}
	
	
}
